package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.glow.changgou.goods.dao.TemplateMapper;
import org.glow.changgou.goods.pojo.Category;
import org.glow.changgou.goods.pojo.Template;
import org.glow.changgou.goods.service.CategoryService;
import org.glow.changgou.goods.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-22 17:06:16
 * @Description
 * @Version 1.0
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements TemplateService {
    @Autowired
    private TemplateService templateService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public Page<Template> findPage(Template template, int page, int size) {
       Page<Template>pageInfo = new Page<>(page,size);
       LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper = getCondition(queryWrapper,template);
        return templateService.page(pageInfo,queryWrapper);
    }

    @Override
    public Page<Template> findPage(int page, int size) {
        Page<Template>pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
        return templateService.page(pageInfo);
    }

    @Override
    public List<Template> findList(Template template) {
        LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,template);
        return templateService.list(queryWrapper);
    }

    @Override
    public void delete(Integer id) {
        templateService.removeById(id);
    }

    @Override
    public void update(Template template) {
            templateService.updateById(template);
    }

    @Override
    public void add(Template template) {
            templateService.save(template);
    }

    @Override
    public Template findById(Integer id) {
        return templateService.getById(id);
    }

    @Override
    public List<Template> findAll() {
        LambdaQueryWrapper<Template> queryWrapper = new LambdaQueryWrapper<>();
        return templateService.list();
    }

    @Override
    public Template findByCategoryId(Integer id) {
        Category category = categoryService.getById(id);

        return templateService.getById(category.getTemplateId());
    }

    public LambdaQueryWrapper<Template> getCondition(LambdaQueryWrapper<Template> queryWrapper,Template template){

        if(template!=null){
            // ID
            if(!StringUtils.isEmpty(template.getId())){
               queryWrapper.eq(Template::getId,template.getId());
            }
            // 模板名称
            if(!StringUtils.isEmpty(template.getName())){
                queryWrapper.like(Template::getName,template.getName());
            }
            // 规格数量
            if(!StringUtils.isEmpty(template.getSpecNum())){
                queryWrapper.eq(Template::getSpecNum,template.getSpecNum());
            }
            // 参数数量
            if(!StringUtils.isEmpty(template.getParaNum())){
                queryWrapper.eq(Template::getParaNum,template.getParaNum());
            }
        }
        return queryWrapper;
    }
}
